package com.gehj.uhttp.other;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Looper;
import android.widget.Toast;

import okhttp3.Response;

/**
  * 与UI交互的类
  * @ClassName:      InteractUI
  * @Description:
  * @Author:         gehj
  * @CreateDate:     2020/12/11 10:27
 */

public class InteractUI {

    /**
     * 401 == token失效
     * 返回首页
     * */
    public  static void BackLogin(Response response, Context context,Class clz) {
        if (null != response) {
            if (response.code() == 401) {
                new Handler(Looper.getMainLooper()).post(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(context, "登录超时,重新登录", Toast.LENGTH_SHORT).show();
                    }
                });
                if (null!=context) {
                    //Intent intent = new Intent(context, LoginActiviy.class);
                    Intent intent = new Intent(context, clz);
                    context.startActivity(intent);
                    if (Activity.class.isInstance(context) ) {
                        ((Activity)context).finish();
                    }
                }

            }/*else{
                new Handler(Looper.getMainLooper()).post(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(context, response.message(), Toast.LENGTH_SHORT).show();
                    }
                });

            }*/
        }
    }

}
